import { selectEntityIdByType, showEntityTablePartial } from '../table'
import type { FlightPlan } from './types'
import { getAircraftFlightPlanInfo, getAirportFlightPlanInfo } from './fpl-index'
import { notify } from '@/widgets'

export function showAircraftFlightPlanTable(e: string) {
  const fplLst = getAircraftFlightPlanInfo(e)
  if (!fplLst || fplLst.length === 0) {
    notify.info({ content: `飞机${e}没有飞行计划!` })
    return
  }
  showEntityTablePartial<FlightPlan>({
    key: `@@fpl-by-ac-${e}`,
    type: 'FlightPlan',
    data: () => fplLst,
    name: `飞机航班 | ${e} (${fplLst.length})`
  })
}

export function showAirportDepartureFlightPlanTable(e: string) {
  const aptInfo = getAirportFlightPlanInfo(e)
  if (!aptInfo.departure || aptInfo.departure.length === 0) {
    notify.info({ content: `机场${e}没有离港航班!` })
    return
  }
  showEntityTablePartial<FlightPlan>({
    key: `@@dep-fpl-by-apt-${e}`,
    type: 'FlightPlan',
    data: () => aptInfo.departure,
    name: `离港航班 | ${e} (${aptInfo.departure.length})`
  })
}

export function showAirportArrivalFlightPlanTable(e: string) {
  const aptInfo = getAirportFlightPlanInfo(e)
  if (!aptInfo.arrival || aptInfo.arrival.length === 0) {
    notify.info({ content: `机场${e}没有进港航班!` })
    return
  }
  showEntityTablePartial<FlightPlan>({
    key: `@@arr-fpl-by-apt-${e}`,
    type: 'FlightPlan',
    data: () => aptInfo.arrival,
    name: `进港航班 | ${e} (${aptInfo.arrival.length})`
  })
}

export function selectAndShowAircraftFlightPlanTable() {
  selectEntityIdByType('Aircraft', showAircraftFlightPlanTable)
}

export function selectAndShowAirportDepartureFlightPlanTable() {
  selectEntityIdByType('Airport', showAirportDepartureFlightPlanTable)
}

export function selectAndShowAirportArrivalFlightPlanTable() {
  selectEntityIdByType('Airport', showAirportArrivalFlightPlanTable)
}
